package org.ilovecode.data {
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import org.ilovecode.utils.Utilities;
	
	/**
	 * GalleryItem class is used to store and load thumbnail and image for gallery.
	 * @author Vjekoslav Ratkajec
	 */
	public class GalleryItem extends EventDispatcher{
		
		private var _name:String = "";
		private var _imagePath:String = "";
		private var _thumbPath:String = "";
		private var _thumb:Sprite = null;
		private var _image:Sprite = null;
		
		private var _thumbHasBound:Boolean = false;
		
		/**
		 * Create instance of GalleryItem class to load image and thumbnail for gallery.
		 * @param	name		Image name.
		 * @param	imagePath	URL path to load image.
		 * @param	thumbPath	URL path to load thumbnail.
		 */
		public function GalleryItem(name:String, imagePath:String, thumbPath:String, thumbHasBound:Boolean = false) {
			_name = name;
			_imagePath = imagePath;
			_thumbPath = thumbPath;
			_thumbHasBound = thumbHasBound;
			
			Utilities.loadContent(thumbPath, loadThumb_Complete);
			Utilities.loadContent(imagePath, loadImage_Complete);
		}
		
		//{ region Load complete
		
		/**
		 * Save loaded thumbnail and check if gallery item is completed.
		 * @param	event	Image loaded.
		 * @private
		 */
		private function loadThumb_Complete(event:Event):void {
			_thumb = getContent(event);
			
			if (_thumbHasBound) {
				_thumb.getChildAt(0).x = (124 - _thumb.getChildAt(0).width) / 2;
				_thumb.graphics.lineStyle(1, 0xADAFB2);
				_thumb.graphics.beginFill(0, 0);
				_thumb.graphics.drawRect(0, 0, 124, _thumb.height);
				_thumb.graphics.endFill();
			}
			_thumb.name = name + "_thumb";
			isComplete();
		}
		
		/**
		 * Save loaded image and check if gallery item is completed.
		 * @param	event	Image loaded.
		 * @private
		 */
		private function loadImage_Complete(event:Event):void {
			_image = getContent(event);
			_image.name = name;
			isComplete();
		}
		
		/**
		 * Check if thumbnail and image are loaded.
		 * If all is loaded, Event.COMPLETE is dispatched.
		 * @private
		 */
		private function isComplete():void {
			if (isImageLoaded && isThumbLoaded) {
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		/**
		 * Extract image or thumbnail from loader.
		 * @param	event	Event containing display object.
		 * @return	Extracted display object placed in Sprite.
		 * @private
		 */
		private function getContent(event:Event):Sprite {
			var loader:Loader = Loader(event.currentTarget.loader);
			var content:DisplayObject = loader.content;
			var result:Sprite = new Sprite();
			result.addChild(content);
			return result;
		}
		
		//} endregion
		
		//{ region Getters
		
		/**
		 * Get image name.
		 */
		public function get name():String {
			return _name;
		}
		
		/**
		 * Get thumbnail graphics.
		 */
		public function get thumb():Sprite {
			return _thumb;
		}
		
		/**
		 * Gat image graphics.
		 */
		public function get image():Sprite {
			return _image;
		}
		
		/**
		 * Check if thumbnail is loaded.
		 * @private
		 */
		private function get isThumbLoaded():Boolean {
			if (_thumb != null) {
				return true;
			}
			return false;
		}
		
		/**
		 * Check if image is loaded.
		 * @private
		 */
		private function get isImageLoaded():Boolean {
			if (_image != null) {
				return true;
			}
			return false;
		}
		
		//} endregion
		
	}

}